import 'dart:io';
import 'dart:typed_data';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:lcapp/api/request/api_client.dart';
import 'package:lcapp/tools/file_tool.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart';
// import 'package:video_compress/video_compress.dart';
import 'package:uuid/uuid.dart';


class ImageCompress {

  static Future<Uint8List> compressToSize({String? imgurl,int size = 32,}) async{

    int maxSize  = size * 1024;
    Uint8List finalPng;
    final dir = await getTemporaryDirectory();
    String path = dir.path + "/compress.png";
    File file = File(path);
    await ApiClient.download(imgurl!, path);
    finalPng = file.readAsBytesSync();
    if (finalPng.lengthInBytes > maxSize) {
      int qa = 90;
      while(finalPng.lengthInBytes > maxSize && qa>1){
        finalPng =  await FlutterImageCompress.compressWithList(
          finalPng,
          minHeight: 100,
          minWidth: 100,
          quality: qa,
        );
        qa = qa -5;
        if(qa<=0){
          qa = 1;
        }
      }
    }
    return finalPng;
  }

  static Future<String> compressToSizeSavePath({File? imgFile,int size = 5,}) async{
    int maxSize  = size * 1024*1000;
    Uint8List finalPng;
    finalPng = imgFile!.readAsBytesSync();
    if (finalPng.lengthInBytes > maxSize) {
      int qa = 90;
      while(finalPng.lengthInBytes > maxSize && qa>1){
        finalPng =  await FlutterImageCompress.compressWithList(
          finalPng,
          quality: qa,
        );
        qa = qa -5;
        if(qa<=0){
          qa = 1;
        }
      }
    }
    Directory saveDir = await getTemporaryDirectory();
    File saveFile = File(join(saveDir.path, 'tmp/${Uuid().v4()}${FileTool.getSuffixName(imgFile.path)}'));
    if (!saveFile.existsSync()) {
      saveFile.createSync(recursive: true);
    }
    saveFile.writeAsBytesSync(
       finalPng,
      flush: true,
    );
    return saveFile.path;
  }

  // static Future<MediaInfo> videocompressToSizeSavePath({File videoFile,int size = 30,int duration,}) async{
  //   MediaInfo info = MediaInfo(path: videoFile.path);
  //   try{
  //      info = await VideoCompress.compressVideo(
  //       videoFile.path,
  //       quality: VideoQuality.DefaultQuality, // default(100)
  //     );
  //   }catch(e){
  //     print('压缩失败==' + e.toString());
  //   }
  //   return info;
  // }

  //获取oss缩略图 固定宽高缩放
  static String reGenerateImageUrl(String url,{int? width, int? height,int quality=100}) {
    if (url != null && url.startsWith('http') && !url.contains('x-oss-process')) {
      if(width!=null&&height!=null){//根据宽高 按比例缩放
        return url + "?x-oss-process=image/format,jpg,image/resize,m_fixed,h_"+height.toString()+",w_"+width.toString()+"/quality,Q_"+quality.toString();
      }else if(width!=null){//根据宽度 按比例缩放
        return url + "?x-oss-process=image/format,jpg,image/resize,m_fixed,w_"+width.toString()+"/quality,Q_"+quality.toString();
      }else if(height!=null){//根据高度 按比例缩放
        return url + "?x-oss-process=image/format,jpg,image/resize,m_fixed,h_"+height.toString()+"/quality,Q_"+quality.toString();
      }else{
        return url;
      }
    } else {
      return url;
    }
  }

}